﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace WPF60.Infrastructure.Converters
{
    public class TimeSpanConverter : IValueConverter
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter">d-day hh-hour mm-minute ss-second</param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var ts = (TimeSpan) value;


            var r =  ts.ToString();

            if (parameter != null)
            {
                switch ((string) parameter)
                {
                    case "d":
                        r = ts.Days.ToString();
                        break;
                    case "hh":
                        r = ts.Hours.ToString("00");
                        break;
                    case "mm":
                        r = ts.Minutes.ToString("00");
                        break;
                    case "ss":
                        r = ts.Seconds.ToString("00");
                        break;
                }

            }

            if (ts < new TimeSpan(0))
            {
                r = "-" + r;
            }

            return r;
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
